//
//  HTUserInfoModel.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/19.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit

class HTUserInfoModel: NSObject,NSCoding{
 

    
    //用户授权的唯一票据
    var access_token :String?
    // 便于判断我们的token是否过期
    var expires_Date: NSDate?
    //access_token的生命周期.单位是秒数
    var expires_in : Int = 0{
        //赋值的时候
        didSet{
            expires_Date = NSDate().dateByAddingTimeInterval(Double(expires_in))
            
        }
    }
    //授权用户的UID
    var uid :String?
    
    //用户头像url
    var avatar_large:String?
    //用户名字
    var screen_name:String?
    
    
    //字典转模型
    init(dict:[String : AnyObject]){
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //为了打印出模型的值
    override var description: String{
        let keys = ["access_token", "expires_Date", "uid", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
        
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder){
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    //解档
    required init?(coder aDecoder: NSCoder){
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid")as?String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }

}
